<?php
/**
 * Description:文件相关
 * User: wuhao
 * Date: 2017-06-26
 * Time: 11:52
 */

namespace app\backend\controller;
use app\backend\controller\traits\AuthManager;
use app\backend\model\Building;
use app\backend\model\ExcelFile;
use app\backend\model\Reconciliation;
use think\Controller;

class FileController extends Controller
{
    use AuthManager;
    private $_memberId = null;
    private $_dtype    = null;
    private $_pdtype   = null;
    private $_pmid     = null;
    private $_allAuth  = [];
    public function __construct()
    {
        parent::__construct();
        $memberInfo = $this->getLoginInfo();
        $this->_memberId = $memberInfo['mid'];
        $this->_dtype    = $memberInfo['dtype'];
        $this->_pdtype   = $memberInfo['p_dtype'];
        $this->_pmid     = $memberInfo['p_mid'];
        $this->_allAuth  = $this->getPower($this->_memberId, $this->_dtype);
    }

    /**
     * 获取上传的文件列表
     * @date   2017-06-07
     * @author wuhao
     *
     * @param string    $begin         上传开始时间
     * @param string    $end           上传截止时间
     * @param int       $type          文件类型
     * @param int       $verifyStatus  验证结果
     * @param int       $status        文件数据是否正确
     * @param int       $page          页数
     * @param int       $pageSize      每页个数
     * @return json
     */
    public function getFile()
    {
        if (!in_array($this->_pdtype, [1,3,4,5,6])) {
            $this->apiReturn(400, [], '无权操作');
        }

        $begin        = I('post.begin', '');
        $end          = I('post.end', '');
        $type         = I('post.type', 1, 'intval');
        $verifyStatus = I('post.verify_status', -1, 'intval');
        $status       = I('post.status', -1, 'intval');
        $page         = I('post.page', 1, 'intval');
        $pageSize     = I('post.page_size', 15, 'intval');
        if (!in_array($verifyStatus, [-1, 0 ,1]) || !in_array($status, [-1,0,1]) || !in_array($type, [1,2,3,4])) {
            $this->apiReturn(400, [], '参数有误');
        }
        $fileModel = new ExcelFile();
        $res = $fileModel->getFileList($this->_pmid, $begin, $end, $type, $verifyStatus, $status, $page, $pageSize);
        if (!$res) {
            $this->apiReturn(400, [], '无数据');
        } else {
            $this->apiReturn(200, $res, '');
        }
    }

    /**
     * @comment  下载对账文件
     * @author wuhao
     */
    public function getReconFile()
    {
        $begin    = I('post.begin');
        $end      = I('post.end');
        $type     = I('post.type', 5, 'intval');
        $page     = I('post.page', 1, 'intval');
        $pageSize = I('post.page_size', 15, 'intval');
        $fileModel = new ExcelFile();
        $res = $fileModel->getFileList(0, $begin, $end, $type, -1, -1, $page, $pageSize);
        if (!$res) {
            $this->apiReturn(400, [], '暂无数据');
        } else {
            $this->apiReturn(200, $res, '');
        }
    }

    /**
     * @comment
     * @author wuhao
     */
    public function getReconResult()
    {
        $begin    = I('post.begin');
        $end      = I('post.end');
        $source   = I('post.source', -1, 'intval');
        $type     = I('post.type', -1, 'intval');
        $result   = I('post.result', -1, 'intval');
        $page     = I('post.page', 1, 'intval');
        $pageSize = I('post.page_size', 15, 'intval');

        if ($begin) {
            $begin = date('Ymd', strtotime($begin));
        }
        if ($end) {
            $end = date('Ymd', strtotime($end));
        }
        //($begin, $end);

        $reconModel = new Reconciliation();
        $res = $reconModel->getResult($begin, $end, $source, $type, $result, $page, $pageSize);
        if (!$res) {
            $this->apiReturn(400, [], '暂无数据');
        } else {
            foreach ($res['list'] as $k => $v) {
                $res['list'][$k]['amount'] = yuan($v['amount']);
            }
            $this->apiReturn(200, $res, '');
        }
    }

    /**
     * @comment  下载文件txt
     * @author wuhao
     */
    public function getFileUrl()
    {
        $id = I('get.id');
        $fileModel = new ExcelFile();
        $res=  $fileModel->getFile($id);
        if (!$res) {
            exit();
        }
        $fileInfo = str_replace(MAIN_DOMAIN, EXCEL_REMOVE_DIR, $res['url']);
        if (!file_exists($fileInfo)) {
            exit('文件不存在');
        }
        //dd($res['url']);
        header("Content-Type: application/force-download");
        header("Content-Disposition: attachment; filename=".basename($fileInfo));
        readfile($fileInfo);

        //echo "<script>location.href='{$res['url']}'</script>";
    }
}